package com.lzy.cloud.goods.facade;

import com.lzy.cloud.goods.dtos.OvertimeBillingDto;
import com.lzy.cloud.goods.dtos.TimeDto;
import com.lzy.cloud.goods.request.OverInfo;
import com.lzy.cloud.goods.request.TimeAndOvertimeBillingRequest;
import com.lzy.cloud.goods.response.TimeAndOvertimeBillingResponse;
import com.lzy.cloud.goods.service.OvertimeBillingService;
import com.lzy.cloud.goods.service.TimeService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 商品超时计费
 *
 * @author lc
 * @version 2018/8/6
 */
@Component
public class OvertimeBillingFacade {
    @Autowired
    private OvertimeBillingService overtimeBillingService;

    @Autowired
    private TimeService timeService;

    /**
     * 商品超时计费和清洁时间一起添加
     * @param request
     * @return
     */
    public Boolean addOvertimeBilling(TimeAndOvertimeBillingRequest request) {
        //添加到商品超时计费表
        OvertimeBillingDto overtimeBillingDto = DozerUtils.map(request, OvertimeBillingDto.class);
        //先判断传进来的参数是否已经存在
        OvertimeBillingDto overDto = new OvertimeBillingDto();
        overDto.setSourceType(overtimeBillingDto.getSourceType());
        overDto.setSourceId(overtimeBillingDto.getSourceId());
        overDto.setShopId(overtimeBillingDto.getShopId());
        List<OvertimeBillingDto> overtimeBillingDtos = overtimeBillingService.selectList(overDto);
        if (overtimeBillingDtos != null) {
            //删除重复
            overtimeBillingService.delRepeat(overtimeBillingDtos.get(0));
        }
        for (OverInfo overInfo : request.getOverInfos()) {
            overtimeBillingDto.setOverTime(overInfo.getOverTime());
            overtimeBillingDto.setOverFee(overInfo.getOverFee());
            overtimeBillingService.addOvertimeBilling(overtimeBillingDto);
        }

        //添加到商品清洁时间表
        TimeDto timeDto = DozerUtils.map(request, TimeDto.class);
        if (1 == timeDto.getType()) {
            TimeDto dto = new TimeDto();
            dto.setShopId(timeDto.getShopId());
            dto.setType(1);
            //一个商家只能有一个批量设置的保洁时间
            List<TimeDto> timeDtos = timeService.selectList(dto);

            Ensure.that(timeDtos).isNotEmpty("00080001");
            timeDto.setType(0);
            timeDto.setCleanTime(timeDtos.get(0).getCleanTime());
            timeDto.setBookingTime(timeDtos.get(0).getBookingTime());
            timeService.addTime(timeDto);
        } else {
            timeService.addTime(timeDto);
        }
        return true;
    }

    /**
     * 商品超时计费和清洁时间一起查询
     * @param request
     * @return
     */
    public TimeAndOvertimeBillingResponse findTimeAndOvertimeBilling(TimeAndOvertimeBillingRequest request) {
        TimeAndOvertimeBillingResponse response = new TimeAndOvertimeBillingResponse();
        TimeDto timeDto = DozerUtils.map(request,TimeDto.class);
        List<TimeDto> timeDtos = timeService.selectList(timeDto);
        //保洁时间
        if (timeDtos.size() > 0){
            response = DozerUtils.map(timeDtos.get(0),TimeAndOvertimeBillingResponse.class);
        }else {
            //数据库中查询不到,新增一条默认的时间
            timeDto.setCleanTime(2);
            timeDto.setBookingTime("09:00-19:00");
            timeService.addTime(timeDto);
            response = DozerUtils.map(timeDto,TimeAndOvertimeBillingResponse.class);
        }
        //超时计费
        OvertimeBillingDto overtimeBillingDto = DozerUtils.map(request,OvertimeBillingDto.class);
        List<OvertimeBillingDto> overtimeBillingDtos = overtimeBillingService.selectList(overtimeBillingDto);
        if (overtimeBillingDtos.size() > 0){
            List<OverInfo> overInfos = DozerUtils.maps(overtimeBillingDtos,OverInfo.class);
            response.setOverInfos(overInfos);
        }
        return response;
    }

    /**
     * 房车租赁下订单 查询超时计费价格
     * @param request
     * @return
     */
    public OvertimeBillingDto findOverTimePrice(TimeAndOvertimeBillingRequest request) {
        OvertimeBillingDto overtimeBillingDto = DozerUtils.map(request,OvertimeBillingDto.class);
        overtimeBillingDto.setOverTime(request.getOverInfos().get(0).getOverTime());
        List<OvertimeBillingDto> overtimeBillingDtos = overtimeBillingService.selectList(overtimeBillingDto);
        if (overtimeBillingDtos.size() > 0) {
            return overtimeBillingDtos.get(0);
        } else {
            return null;
        }
    }
}
